#include <string.h>
#include <stdio.h>

#import <msado15.dll> rename( "EOF", "adoEOF")

// Initialization of OLE
struct InitOle {
  InitOle()  { ::CoInitialize(NULL); }
  ~InitOle() { ::CoUninitialize();   }
} _init_InitOle_;       // Global Instance to force load/unload of OLE

void LogCrackHR( HRESULT hr, char** strTmp );



void Con1_Open( char *lpszConnectString )
{
    HRESULT                 hr        = S_OK;
    BSTR                    bstrEmpty = L"";
    ADODB::_ConnectionPtr   conn1;

    try
    {
        hr = conn1.CreateInstance(__uuidof( ADODB::Connection));
        if ( FAILED ( hr ) ) throw 0L;

        conn1->ConnectionString = _bstr_t( lpszConnectString );
        conn1->Open( bstrEmpty, bstrEmpty, bstrEmpty, -1 );
        conn1->Close();
    
        printf( "\t...Success!\n" );    
    }
    catch(...)
    {
        char    strTmp[255];
        char*   strPtr = &strTmp[0];

        LogCrackHR( hr, &strPtr );

        printf( "\t...Oops!  We failed to connect! (%s)\n",
                strTmp );
    }
    conn1 = NULL;
}

void main( void )
{
    printf ( "DSN Connection to ODBC Drivers:\n\n" );

    printf ( "Access ODBC Driver...\n" );
    {
        // This code assumes that the ODBC Data Source OLE_DB_NWind_Jet
        // was created by the installation of the DATA ACCESS 2.0 SDK

        // Access ODBC Driver via DSN
        Con1_Open( "PROVIDER=MSDASQL;" 
                   "DSN=OLE_DB_NWind_Jet;" 
                   "UID=admin;PWD=;" );

        Con1_Open( "DSN=OLE_DB_NWind_Jet;" 
                   "UID=admin;PWD=;" );

        // Access ODBC Driver via DSN using ADO Syntax
        // Note use of DATA SOURCE=, USER ID=, and PASSWORD= clauses
        Con1_Open( "PROVIDER=MSDASQL;" 
                   "DATA SOURCE=OLE_DB_NWind_Jet;" 
                   "USER ID=admin;PASSWORD=;" );

        Con1_Open( "DATA SOURCE=OLE_DB_NWind_Jet;" 
                   "USER ID=admin;PASSWORD=;" );
    }

    printf ( "Oracle ODBC Driver...\n" );
    {
        // This code assumes you have Oracle Client software installed,
        // that you have an oracle Server named "dseOracle8" and that
        // you have an ODBC Data Source named "dseOracleDSN" that
        // references this server.
    
        // Oracle ODBC Driver via DSN
        Con1_Open ("PROVIDER=MSDASQL;" 
                  "DSN=dseOracleDSN;" 
                  "UID=demo;PWD=demo;" );
    
        Con1_Open ("DSN=dseOracleDSN;" 
                  "UID=demo;PWD=demo;" );
    
        // Oracle ODBC Driver via DSN using ADO Syntax
        // Note use of DATA SOURCE=, USER ID=, and PASSWORD= clauses
        Con1_Open ("PROVIDER=MSDASQL;" 
                  "DATA SOURCE=dseOracleDSN;" 
                  "USER ID=demo;PASSWORD=demo;" );
    
        Con1_Open ("DATA SOURCE=dseOracleDSN;" 
                  "USER ID=demo;PASSWORD=demo;" );
    }

    printf ( "SQL Server ODBC Driver...\n" );
    {
        // This code assumes you have SQL Server installed with
        // an ODBC Data Source named "LocalServer"
  
        // SQL Server ODBC Driver via DSN
        Con1_Open ("PROVIDER=MSDASQL;" 
                  "DSN=LocalServer;database=Master;" 
                  "UID=sa;pwd=;" );
              
        Con1_Open ("DSN=LocalServer;database=Master;" 
                  "UID=sa;pwd=;" );
              
        // SQL Server ODBC Driver via DSN using ADO syntax
        // Note use of DATA SOURCE=, INITIAL CATALOG=, USER ID=, and PASSWORD= clauses
        Con1_Open ("PROVIDER=MSDASQL;" 
                  "DATA SOURCE=LocalServer;INITIAL CATALOG=pubs;" 
                  "USER ID=sa;PASSWORD=;" );
    
        Con1_Open ("DATA SOURCE=LocalServer;INITIAL CATALOG=pubs;" 
                  "USER ID=sa;PASSWORD=;" );
    }

    printf ( "\nDSN-Less Connection String to ODBC Drivers:\n\n" );

    printf ( "Access ODBC Driver...\n" );
    {
        // This code assumes you have the NWIND access database
        // in a path C:\_data

        //  Access ODBC Driver via DSN-Less
        Con1_Open ("PROVIDER=MSDASQL;" 
                  "DRIVER={Microsoft Access Driver (*.mdb)};" 
                  "DBQ=C:\\_data\\NWind.mdb;" 
                  "UID=sa;PWD=;" );

        Con1_Open ("DRIVER={Microsoft Access Driver (*.mdb)};" 
                  "DBQ=C:\\_data\\NWind.mdb;" 
                  "UID=sa;PWD=;" );
    }

    printf ( "Oracle ODBC Driver...\n" );
    {
        // This code assumes you have Oracle Client software installed,
        // that you have an oracle Server named "dseOracle8"
   
        //  Oracle ODBC Driver
        Con1_Open ("PROVIDER=MSDASQL;" 
                  "DRIVER={Microsoft ODBC for Oracle};" 
                  "SERVER=dseOracle8;" 
                  "UID=demo;PWD=demo;" );

        Con1_Open ("DRIVER={Microsoft ODBC for Oracle};" 
                  "SERVER=dseOracle8;" 
                  "UID=demo;PWD=demo;" );
    }

    printf ( "SQL Server ODBC Driver...\n" );
    {
        // This code assumes you have SQL Server named "zhadumnt"

        //  SQL Server ODBC Driver
        Con1_Open ("PROVIDER=MSDASQL;" 
                  "DRIVER={SQL Server};" 
                  "SERVER=zhadumnt;DATABASE=pubs;" 
                  "UID=sa;PWD=;" );

        Con1_Open ("DRIVER={SQL Server};" 
                  "SERVER=zhadumnt;DATABASE=pubs;" 
                  "UID=sa;PWD=;" );
    }

    printf ( "\nConnection String to Native OLE DB Providers:\n\n" );

    printf ( "Jet OLE DB Native Provider...\n" );
    {
        // This code assumes you have the NWIND access database
        // in a path C:\_data

        // Access Provider
        // NOTE:  UID and PWD syntax cannot be used here
        Con1_Open ("PROVIDER=Microsoft.Jet.OLEDB.4.0;" 
                  "DATA SOURCE=C:\\_data\\NWind.mdb;" 
                  "USER ID=admin;PASSWORD=;" );
    }

    printf ( "Oracle OLE DB Native Provider...\n" );
    {
        // This code assumes you have Oracle Client software installed,
        // that you have an oracle Server named "dseOracle8"

        //  Oracle Provider
        Con1_Open ("PROVIDER=MSDAORA;" 
                  "DATA SOURCE=dseOracle8;" 
                  "USER ID=demo;PASSWORD=demo;" );
    }

    printf ( "SQL Server OLE DB Native Provider...\n" );
    {
        // This code assumes you have SQL Server named "zhadumnt"
    
        //  SQL Server Provider
        Con1_Open ("PROVIDER=SQLOLEDB;" 
                  "DATA SOURCE=zhadumnt;DATABASE=pubs;" 
                  "USER ID=sa;PASSWORD=;" );
    
        //  SQL Server Provider (using ODBC Syntax)
        Con1_Open ("PROVIDER=SQLOLEDB;" 
                  "DSN=zhadumnt;DATABASE=pubs;" 
                  "UID=sa;PWD=;" );
    }
}

void LogCrackHR( HRESULT hr, char** strTmp )
{
    LPVOID  lpMsgBuf;

    ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
                     NULL,
                     hr,
                     MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
                     (LPTSTR) &lpMsgBuf,
                     0,
                     NULL ); 


    sprintf( *strTmp, "%s", (char *) lpMsgBuf );

    for ( long i = 0; i < 255; i++ )
    {
        if ( (*strTmp)[i] == '\r' )
            (*strTmp)[i] = ' ';
        if ( (*strTmp)[i] == '\n' )
            (*strTmp)[i] = ' ';
    }

    // Free the buffer.
    ::LocalFree( lpMsgBuf );
}

